package com.hui;

import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.*;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.job.flow.JobExecutionDecider;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.item.*;
import org.springframework.batch.item.support.ListItemReader;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.security.PublicKey;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * @author zhang zhongtao
 */
@Configuration
@Slf4j
public class SpringbatchLearn11JobParamsConfiguration implements StepExecutionListener {
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Bean
    public Job myParamsJob() {
        return jobBuilderFactory.get("myParamsJob")
                .start(myParamsStep())
                .build();
    }

    @Bean
    public Step myParamsStep() {
        return stepBuilderFactory.get("myParamsStep")
                .listener(this)
                .tasklet((stepContribution, chunkContext) -> {
                    log.info("\n\r{}" , chunkContext.getStepContext().getJobParameters().toString());
                    return RepeatStatus.FINISHED;
                }).build();
    }


    @Override
    public void beforeStep(StepExecution stepExecution) {
        Map<String, JobParameter> parameters = stepExecution.getJobParameters().getParameters();
        StringBuilder sb = new StringBuilder();
        parameters.forEach((s, jobParameter) -> {
            sb.append(s + "=" + jobParameter.toString());
        });
        log.info("\n\rSpringbatchLearn11JobParamsConfiguration.beforeStep {},parameters:{}", stepExecution.getStepName(), sb.toString());
    }

    @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        log.info("\n\rSpringbatchLearn11JobParamsConfiguration.afterStep");
        return null;
    }
}
